/*
 *      dxirc.h
 *
 *      Copyright 2008 David Vachulka <david@konstrukce-cad.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */


#ifndef DXIRC_H
#define DXIRC_H

#ifndef PREFERENCES_H
#include "preferences.h"
#endif

#ifndef FX_TRAY_ICON_H
#include "FXTrayIcon.h"
#endif


class dxTabBook;
class LogViewer;
class DccDialog;
#ifndef HAVE_LIBNOTIFY
class dxEXNotify;
#endif
class dxSocket;

class dxirc: public FXMainWindow
{
    FXDECLARE(dxirc)
    public:
        dxirc(FXApp *app);
        virtual ~dxirc();

        void create();
        void flash(FXbool yes);
        void showNotify(const FXString& notify, FXint pos=-1);
        static dxirc* instance();
        dxDccFilesArray & getDccFilesList() { return m_dccfilesList; }
        dxScriptsArray & getScripts() { return m_scripts; }
        FXint loadLuaScript(FXString, FXbool=TRUE);
        FXint unloadLuaScript(FXString);
        Preferences & getPreferences() { return m_preferences; }

        long onCmdQuit(FXObject*, FXSelector, void*);
        long onCmdAbout(FXObject*, FXSelector, void*);
        long onCmdHelp(FXObject*, FXSelector, void*);
        long onCmdServers(FXObject*, FXSelector, void*);
        long onCmdConnect(FXObject*, FXSelector, void*);
        long onCmdDisconnect(FXObject*, FXSelector, void*);
        long onCmdReconnect(FXObject*, FXSelector, void*);
        long onIrcEvent(FXObject*, FXSelector, void*);
        long onDccEvent(FXObject*, FXSelector, void*);
        long onTabBook(FXObject*, FXSelector, void*);
        long onCmdClear(FXObject*, FXSelector, void*);
        long onCmdClearAll(FXObject*, FXSelector, void*);
        long onCmdCloseTab(FXObject*, FXSelector, void*);
        long onCmdSelectTab(FXObject*, FXSelector, void*);
        long onCmdNextTab(FXObject*, FXSelector, void*);
        long onCmdMoveTab(FXObject*, FXSelector, void*);
        long onCmdNextUnread(FXObject*, FXSelector, void*);
        long onCmdUsers(FXObject*, FXSelector, void*);
        long onCmdStatus(FXObject*, FXSelector, void*);
        long onCmdOptions(FXObject*, FXSelector, void*);
        long onCmdAlias(FXObject*, FXSelector, void*);
        long onCmdLog(FXObject*, FXSelector, void*);
        long onCmdTransfers(FXObject*, FXSelector, void*);
        long onCmdClose(FXObject*, FXSelector, void*);
        long onCmdDccCancel(FXObject*, FXSelector, void*);
        long onMouseWheel(FXObject*, FXSelector, void*);
        long onTabConnect(FXObject*, FXSelector, void*);
        long onTrayClicked(FXObject*, FXSelector, void*);
        long onNewMsg(FXObject*, FXSelector, void*);
        long onNewTetris(FXObject*, FXSelector, void*);
        long onTetrisKey(FXObject*, FXSelector, void*);
        long onLua(FXObject*, FXSelector, void*);
        long onCmdScripts(FXObject*, FXSelector, void*);
        long onIrcCommand(FXObject*, FXSelector, void*);
        long onIrcMyMsg(FXObject*, FXSelector, void*);
        long onStatusTimeout(FXObject*, FXSelector, void*);
        long onAddIgnoreCommand(FXObject*, FXSelector, void*);
        long onRemoveIgnoreCommand(FXObject*, FXSelector, void*);
        long onAddIgnoreUser(FXObject*, FXSelector, void*);
        long onRemoveIgnoreUser(FXObject*, FXSelector, void*);
        long onCmdSpell(FXObject*, FXSelector, void*);
        long onCmdForceFocus(FXObject*, FXSelector, void*);
        long onAwayTimeout(FXObject*, FXSelector, void*);
        long onCmdCheckVersion(FXObject*, FXSelector, void*);
        long onVersionSocketConnected(FXObject*, FXSelector, void*);
        long onVersionSocketCanRead(FXObject*, FXSelector, void*);
        long onVersionSocketError(FXObject*, FXSelector, void*);
        long onShowLog(FXObject*, FXSelector, void*);
        static int onLuaAddCommand(lua_State*);
        static int onLuaAddEvent(lua_State*);
        static int onLuaAddMyMsg(lua_State*);
        static int onLuaAddNewTab(lua_State*);
        static int onLuaAddDxircQuit(lua_State*);
        static int onLuaAddAll(lua_State*);
        static int onLuaRemoveName(lua_State*);
        static int onLuaCommand(lua_State*);
        static int onLuaPrint(lua_State*);
        static int onLuaGetServers(lua_State*);
        static int onLuaGetTab(lua_State*);
        static int onLuaGetCurrentTab(lua_State*);
        static int onLuaGetVersion(lua_State*);
        static int onLuaGetTabInfo(lua_State*);
        static int onLuaSetTab(lua_State*);
        static int onLuaCreateTab(lua_State*);
        static int onLuaGetTabCount(lua_State*);
        static int onLuaClear(lua_State*);

    private:
        dxirc(){}
        static dxirc *_pThis;
        FXApp *m_app;
        dxScriptsArray m_scripts;
        dxDccFilesArray m_dccfilesList;
        FXFont *m_ircFont;
        FXPopup *m_traymenu;
        FXTrayIcon *m_trayIcon;
        FXint m_lastToken;
        FXuint m_lastID;
        dxSmileyArray m_smileys;
        FXMenuBar *m_menubar;
        FXMenuPane *m_servermenu, *m_helpmenu, *m_editmenu;
        FXMenuCommand *m_disconnect, *m_reconnect, *m_closeTab, *m_clearTab, *m_clearTabs, *m_logviewer;
        FXMenuCheck *m_users, *m_status;
#ifdef HAVE_ENCHANT
        FXMenuCheck *m_spellCombo;
#endif
        FXHorizontalFrame *m_statusbar;
        FXLabel *m_statuslabel;
        FXVerticalFrame *m_mainframe;
        dxTabBook *m_tabbook;
        LogViewer *m_viewer;
        DccDialog *m_transfers;
        dxIrcEnginesArray m_ircengines;
        dxDccEnginesArray m_dccengines;
        dxScriptEventsArray m_scriptEvents;
#ifndef HAVE_LIBNOTIFY
        dxEXNotify *m_wnotify;
#endif
        dxSocket *m_versionSocket;
        Preferences m_preferences;

        void onIrcNewchannel(IrcEngine*, IrcEvent*);
        void onIrcQuery(IrcEngine*, IrcEvent*);
        void onIrcPart(IrcEngine*, IrcEvent*);
        void onIrcKick(IrcEngine*, IrcEvent*);
        void onIrcDisconnect(IrcEngine*, IrcEvent*);
        void onIrcConnectAndReconnect(IrcEvent*);
        void onIrcEndmotd();
        void onIrcPrivmsgAndAction(IrcEngine*, IrcEvent*);
        void onIrcInvite(IrcEngine*, IrcEvent*);
        void onIrcJoin(IrcEngine*, IrcEvent*);
        void onIrcQuit(IrcEngine*, IrcEvent*);
        void onIrcDccChat(IrcEngine*, IrcEvent*);
        void onIrcDccServer(IrcEngine*, IrcEvent*);
        void onIrcDccIn(IrcEngine*, IrcEvent*);
        void onIrcDccOut(IrcEngine*, IrcEvent*);
        void onIrcDccPout(IrcEngine*, IrcEvent*);
        void onIrcDccMyToken(IrcEngine*, IrcEvent*);
        void onIrcDccToken(IrcEngine*, IrcEvent*);
        void onIrcDccPosition(DccEngine*, IrcEvent*);
        void onIrcDccResume(IrcEngine*, IrcEvent*);
        void onIrcDccPresume(IrcEngine*, IrcEvent*);
        void onIrcDccAccept(IrcEngine*, IrcEvent*);
        void onIrcDccPaccept(IrcEngine*, IrcEvent*);
        FXbool tabExist(IrcEngine*, FXString);
        FXbool serverExist(FXString, FXint, FXString);
        FXint getServerTab(IrcEngine*);
        FXint getTabId(IrcEngine*, FXString);
        FXint getTabId(FXString);
        FXint getCurrentTabId();
        void setCurrentTabById(FXint id);
        FXWindow* getTabItemById(FXint id);
        FXbool isValidTabId(FXint id);
        FXbool isIddxTabItem(FXint id);
        FXbool isLastTab(IrcEngine*);
        FXbool isFriend(FXString nick, FXString on, FXString network);
        void connectServer(FXString hostname, FXint port, FXString pass, FXString nick, FXString rname, FXString channels, FXString commands, FXbool ssl);
        void updateMenus();
        void updateTheme(FXColor oldUnread, FXColor oldHighlight);
        void updateTrayColor();
        void updateTabs(FXbool recreateSmileys=FALSE);
        void updateTabPosition();
        void updateFont();
        void updateStatus(FXString);
        void updateNickIcons();
        void updateServerWindow();
        FXbool hasTetrisTab();
        void sortTabs();
        void moveTab(bool up);
        void appendIrcText(FXString text, FXbool logLine=TRUE);
        void appendIrcStyledText(FXString text, FXint style, FXbool logLine=TRUE);
        FXbool hasLuaAll(const FXString &);
        FXbool hasAllCommand();
        FXbool hasMyMsg();
        void autoloadScripts();
        void sendNewTab(IrcEngine *server, const FXString &name, FXint id, FXbool isTetris, FXbool isDccTab, TYPE type);
        void createSmileys();
        FXString getUniqueName(const FXString &path, const FXString &name, const FXString &extension);
        FXbool isForResume(const FXString &name);
        FXint createIrcTab(const FXString &tabtext, FXIcon *icon, TYPE typ, IrcEngine *engine);
        void createDccTab(const FXString &mynick, const FXString &nick, const FXString &address="0", FXint portD=0, FXint portH=0, FXbool listen=FALSE, IrcEngine *engine=NULL);
};

typedef void (dxirc::* func_ptr) (const FXString&);

#endif // DXIRC_H
